package application.view.viewComponent 
{
	import application.model.VO.GroupVO;
	import application.model.VO.PhotoViewVO;
	import application.model.VO.PhotoVO;
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.events.ProgressEvent;
	import flash.net.URLRequest;
	import caurina.transitions.Tweener;
	import flash.system.LoaderContext;
	import flash.utils.setTimeout;
	import ginger.events.DataEvent;
	import ginger.ui.simple.SimpleLayout;
	import ginger.utils.DisplayUtil;
	/**
	 * ...
	 * @author Ning
	 */
	public class PhotoView extends LayoutView
	{
		public static const EXIT:String = 'exit';
		public static const LEAVE_COMPLETE:String = 'leaveComplete';
		
		private var _data:Array;
		private var _loader:Loader;
		private var _bitmap:Bitmap;
		private var _prevBtn:PrevBtn;
		private var _nextBtn:NextBtn;
		private var _bitmapBox:Sprite;
		private var _curIndex:int;
		private var _loading:Loading;
		private var _exitBtn:ExitBtn;
		private var _childInfoBtn:SimpleButton;
		private var _infoPanel:InfoPanel;
		private var _config:PhotoViewVO;
		
		
		private var _showPrevBtn:Boolean=true;
		private var _showNextBtn:Boolean=true;
		/**
		 * 是否展示prevBtn
		 */
		private function set showPrevBtn(value:Boolean):void {
			//同样情况返回
			if (showPrevBtn == value)
			    return;
			_showPrevBtn = value;
			if (showPrevBtn) {
				_prevBtn.x = -_prevBtn.hotAreaWidth;
				Tweener.addTween(_prevBtn, { time:1, x:0,transition:'easeOutSine' } );
			}
			else {
				Tweener.addTween(_prevBtn, { time:1, x: -_prevBtn.hotAreaWidth } );
			}
		}
		private function get showPrevBtn():Boolean {
			return _showPrevBtn;
		}
		
		/**
		 * 是否展示nextBtn
		 */
		private function set showNextBtn(value:Boolean):void {
			//同样情况返回
			if (showNextBtn == value)
			    return;
			_showNextBtn = value;
			if (showNextBtn) {
				_nextBtn.x = AppGlobal.STAGE.stageWidth;
				Tweener.addTween(_nextBtn, { time:1, x:AppGlobal.STAGE.stageWidth - _nextBtn.hotAreaWidth,transition:'easeOutSine' } );
			}
			else {
				Tweener.addTween(_nextBtn, { time:1, x: AppGlobal.STAGE.stageWidth } );
			}
		}
		private function get showNextBtn():Boolean {
			return _showNextBtn;
		}
		
		//图片数量
		private function get count():int {
			return _data.length;
		}
		

		public function PhotoView() 
		{
			init();
		}
		private function init():void
		{
			_loader = new Loader();
			_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
			_loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgress);
			_loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onIoError);
			
			_bitmapBox = new Sprite();
			addChild(_bitmapBox);
			
			
			_exitBtn = new ExitBtn();
			addChild(_exitBtn);
			
			_loading = new Loading();
			_exitBtn.addEventListener(MouseEvent.CLICK, onExitClick);
			
			_childInfoBtn = AppGlobal.MATERIAL_RESOURCES.childInfoBtn;
			_childInfoBtn.addEventListener(MouseEvent.CLICK, onChildInfo);
			addChild(_childInfoBtn);
			
			_infoPanel = new InfoPanel();
			_infoPanel.addEventListener(MouseEvent.ROLL_OUT, onInfoPanelOut);
			
			
		}
		override public function layout():void 
		{
			if (_bitmap) {
				setPhotoSize(_bitmap);
			}
			
			if (contains(_loading))
			    _loading.resize();
			
			if (showPrevBtn)
				_prevBtn.x = 0; 
			else 
			    _prevBtn.x = -_prevBtn.hotAreaWidth;
			
			if (showNextBtn)
				_nextBtn.x = AppGlobal.STAGE.stageWidth - _nextBtn.hotAreaWidth;   
		    else 
			    _nextBtn.x = AppGlobal.STAGE.stageWidth;
				
			_prevBtn.y = (AppGlobal.STAGE.stageHeight - _prevBtn.hotAreaHeight) / 2;
			_nextBtn.y = (AppGlobal.STAGE.stageHeight - _nextBtn.hotAreaHeight) / 2;
			
			SimpleLayout.layout(_childInfoBtn, AppGlobal.STAGE, SimpleLayout.TOP, NaN, NaN, 0);
			SimpleLayout.layout(_infoPanel, AppGlobal.STAGE, SimpleLayout.TOP, NaN, NaN, 0);
			
			_exitBtn.layout();
		}
		public function updateData(data:Array):void
		{
			_data = data;
		}
		public function updateConfig(config:PhotoViewVO):void
		{
			_config = config;
			
			_exitBtn.updateLayout(_config.exitBtnLayout);
			
			_prevBtn = new PrevBtn(config);
			_nextBtn = new NextBtn(config);
			
			addChild(_prevBtn);
			addChild(_nextBtn);
			
			showNextBtn = false;
			showPrevBtn = false;
			
			_prevBtn.addEventListener(MouseEvent.CLICK, onPrevClick);
			_nextBtn.addEventListener(MouseEvent.CLICK, onNextClick);
			
			layout();
			
		}
		
		public function showPhotoByIndex(index:int):void
		{
			if (!contains(_loading))
			    addChild(_loading);
			_loading.reset();
			
			_curIndex = index;
			
			var vo:PhotoVO = _data[index] as PhotoVO;
			_loader.load(new URLRequest(vo.url), new LoaderContext(true));
			_infoPanel.setData(vo);
			
			_prevBtn.reset();
			_nextBtn.reset();
			
			if (count == 1) {
				showNextBtn = false;
				showPrevBtn = false;
			}
			else if (index == 0) {
				showNextBtn = true;
				showPrevBtn = false;
				vo = _data[index + 1] as PhotoVO;
				_nextBtn.setData(vo.url1);
			}
			else if (index == count - 1) {
				showNextBtn = false;
				showPrevBtn = true;
				vo = _data[index - 1] as PhotoVO;
				_prevBtn.setData(vo.url1);
			}
			else {
				showNextBtn = true;
				showPrevBtn = true;
				
				vo = _data[index + 1] as PhotoVO;
				_nextBtn.setData(vo.url1);
				
				vo = _data[index - 1] as PhotoVO;
				_prevBtn.setData(vo.url1);
			}
		}
		public function show():void
		{
			Tweener.addTween(_exitBtn, { time:.5, alpha:1 } );
			layout();
		}
		/**
		 * 离开
		 */
		public function leave():void
		{
			Tweener.addTween(_bitmap, { time:.5,y:AppGlobal.STAGE.stageHeight,onComplete:onComplete } );
			Tweener.addTween(_exitBtn, { time:.5,alpha:0 } );
			function onComplete():void
			{
				dispatchEvent(new Event(PhotoView.LEAVE_COMPLETE));
			}
		}
		
		private function onProgress(evt:ProgressEvent):void
		{
			_loading.text = String(Math.ceil((evt.bytesLoaded / evt.bytesTotal) * 100))+'%';
		}
        private function onIoError(evt:IOErrorEvent):void
		{
			
		}
		
		private function onComplete(evt:Event):void
		{
			//关闭loading
			Tweener.addTween(_loading, { time:.5, alpha:0, onComplete:onLoadingComplete } );
			
			
			var bmp:Bitmap = evt.target.content as Bitmap;
			bmp.smoothing = true;
			_bitmapBox.addChild(bmp);
			
			//设置更新图片的大小
			setPhotoSize(bmp);
			
			//新加载的图片向下缓动
			bmp.y = -bmp.height;
			Tweener.addTween(bmp, { time:1, y:0,onComplete:onComplete } );
			
			function onComplete():void
			{
				if(_bitmap)
				    _bitmap.bitmapData.dispose();
				_bitmap = this;
			}
			function onLoadingComplete():void
			{
				if (contains(this))
			        removeChild(this);
			}
		}
		
		/**
		 * 设置图片的大小和x位置居中
		 * @param	photo
		 */
		private function setPhotoSize(photo:Bitmap):void
		{
			DisplayUtil.setTheMostSuitableSize(photo, AppGlobal.STAGE.stageWidth, AppGlobal.STAGE.stageHeight);
			photo.x = (AppGlobal.STAGE.stageWidth - photo.width) / 2;
		}
		
		private function onPrevClick(evt:MouseEvent):void
		{
			var index:int = _curIndex - 1;
			if (index == -1)
		        index = count - 1;
			transition(index);
			
		}
		private function onNextClick(evt:MouseEvent):void
		{
			var index:int = _curIndex + 1;
			if (index == count)
		        index = 0;
			transition(index);
		}
		private function onExitClick(evt:MouseEvent):void
		{
			dispatchEvent(new Event(EXIT));
		}
		/**
		 * 
		 * @param	index
		 */
		private function transition(index:int):void
		{
			showNextBtn = false;
			showPrevBtn = false;

			setTimeout(showPhotoByIndex, 1000, index);
		}
		
		//展示信息
		private function onChildInfo(evt:MouseEvent=null):void
		{
			Tweener.addTween(_childInfoBtn, { time:.5, y: -_childInfoBtn.height } );
			if (!contains(_infoPanel))
			    addChild(_infoPanel);
			_infoPanel.y = -_infoPanel.height;
			Tweener.removeTweens(_infoPanel);
			Tweener.addTween(_infoPanel, { time:.5, y: 0 } );
		}
		
		//信息面板离开
		private function onInfoPanelOut(evt:MouseEvent = null):void
		{
			Tweener.addTween(_infoPanel, { time:.5, y: -_infoPanel.height, onComplete:onComplete } );
			Tweener.addTween(_childInfoBtn, { time:.5, delay:.5,y: 0 } );
			function onComplete():void
			{
				if (contains(this))
				    removeChild(this);
			}
		}
	}
}

import application.model.VO.PhotoViewVO;
import application.model.VO.PhotoVO;
import flash.display.Bitmap;
import flash.display.Loader;
import flash.display.Shape;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import caurina.transitions.Tweener;
import flash.filters.BlurFilter;
import flash.net.URLRequest;
import flash.text.TextFormat;
import ginger.ui.simple.SimpleLabel;
import ginger.utils.DisplayUtil;
import application.view.viewComponent.LayoutView;
class ExitBtn extends LayoutView
{
	private var _exitBmp:Bitmap;
	public function ExitBtn()
    {
		init();
	}
	private function init():void
	{
		var mresources:MaterialResources = AppGlobal.MATERIAL_RESOURCES;
		_exitBmp = new Bitmap(mresources.photoViewExitBtnBmpData);
		addChild(_exitBmp);
		
		this.buttonMode = true;
	}
}
class Loading extends Sprite
{
	private var _preLabel:SimpleLabel;
	//数字的样式
	private var _format:TextFormat;
	//%的样式
	private var _format1:TextFormat;
	
	public function Loading()
	{
		init();
	}
	private function init():void {
		_preLabel = new SimpleLabel();
		_format = new TextFormat('微软雅黑', 70, 0, true);
		_format1 = new TextFormat('微软雅黑', 70, 0);
		_preLabel.filters = [new BlurFilter(1.2, 1.2)];
		addChild(_preLabel);
	}
	public function set text(value:String):void 
	{
		_preLabel.text = value;
		_preLabel.setTextFormat(_format, 0, _preLabel.length - 1);
		_preLabel.setTextFormat(_format1, _preLabel.length - 1, _preLabel.length);
	}
	public function reset():void
	{
		this.text = '0%';
		this.alpha = 1;
		resize();
	}
	public function resize():void
	{
		graphics.clear();
		graphics.beginFill(0xffffff, .3);
		graphics.drawRect(0, 0, AppGlobal.STAGE.stageWidth, AppGlobal.STAGE.stageHeight);
		graphics.endFill();
		
		_preLabel.x = (AppGlobal.STAGE.stageWidth - _preLabel.width) / 2;
		_preLabel.y = (AppGlobal.STAGE.stageHeight - _preLabel.height) / 2;
	}
}
/**
 *按钮基类
 */
class BasicBtn extends Sprite
{
	private var _bitmap:Bitmap;
	
	protected var hotArea:Sprite;
	protected var hotBtn:Sprite;
	protected var bitmapBox:Sprite;
	protected var bitmapMask:Shape;
	
	private var _hotAreaWidth:Number;
	private var _hotAreaHeight:Number;
	
	private var _hotBtnWidth:Number;
	private var _hotBtnHeight:Number;
	
	private var _hotBimapWidth:Number;
	private var _hotBimapHeight:Number;
	
	private var _loader:Loader;
	
	//热区宽
	public function get hotAreaWidth():Number {
		return _hotAreaWidth;
	}
	
	//热区高
	public function get hotAreaHeight():Number {
		return _hotAreaHeight;
	}
	
	//热区按钮宽
	protected function get hotBtnWidth():Number {
		return _hotBtnWidth;
	}
	
	//热区按钮高s
	protected function get hotBtnHeight():Number {
		return _hotBtnHeight;
	}
	
	//热区图片宽
	protected function get hotBimapWidth():Number {
		return _hotBimapWidth;
	}
	
	//热区图片高
	protected function get hotBimapHeight():Number {
	    return _hotBimapHeight;
	}

	public function BasicBtn()
	{
		
	}
	public function setConfig(hotAreaWidth:Number,hotAreaHeight:Number,hotBtnWidth:Number,hotBtnHeight:Number,hotBimapWidth:Number,hotBimapHeight:Number):void
	{
		_hotAreaWidth = hotAreaWidth;
		_hotAreaHeight = hotAreaHeight;
		_hotBtnWidth = hotBtnWidth;
		_hotBtnHeight = hotBtnHeight;
		_hotBimapWidth = hotBimapWidth;
		_hotBimapHeight = hotBimapHeight;
		
		updateUI()
	}
	
	/**
	 * 重置
	 */
	public function reset():void
	{
		//破坏原有图片
		if (_bitmap) {
			if (bitmapBox.contains(_bitmap))
			    bitmapBox.removeChild(_bitmap);
		    _bitmap.bitmapData.dispose();
		}
	}
	
	public function setData(url:String):void
	{
		_loader.load(new URLRequest(url));
	}
	private function updateUI():void
	{
		hotArea = new Sprite();
		addChild(hotArea);
		
		//创建热区空间
		graphics.beginFill(0, 0);
		graphics.drawRect(0, 0, hotAreaWidth, hotAreaHeight);
		graphics.endFill();	
		
		hotBtn = new Sprite();
		hotArea.addChild(hotBtn);
		hotBtn.y = (hotAreaHeight - hotBtnHeight) / 2;
		
		bitmapBox = new Sprite();
		bitmapBox.y = (hotAreaHeight - hotBimapHeight) / 2;

		//图片容器的遮罩
		bitmapMask = new Shape();
		bitmapMask.graphics.beginFill(0xffffff, 0);
		bitmapMask.graphics.drawRect(0, 0, hotBimapWidth-1, hotBimapHeight-1);
		bitmapMask.y = bitmapBox.y + 1;

		hotArea.addChild(bitmapMask);
		
		buttonMode = true;
		
		//全区
		addEventListener(MouseEvent.ROLL_OVER, onOver);
		addEventListener(MouseEvent.ROLL_OUT, onOut);
		
		//热区
		hotArea.addEventListener(MouseEvent.ROLL_OVER, onHotAreaOver);
		hotArea.addEventListener(MouseEvent.ROLL_OUT, onHotAreaOut);
		
		onHotAreaOut();
		
		_loader = new Loader();
		_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
	}
	private function updateHotBtnGraphics(fillColor:uint,lineColor:uint):void
	{
		hotBtn.graphics.clear();
		hotBtn.graphics.beginFill(fillColor,1);
		hotBtn.graphics.drawRect(0, 0, hotBtnWidth, hotBtnHeight);
		hotBtn.graphics.endFill();
		hotBtn.graphics.lineStyle(2, lineColor);
		hotBtn.graphics.moveTo((hotBtnWidth - 8) / 2, hotBtnHeight / 2);
		hotBtn.graphics.lineTo((hotBtnWidth - 8) / 2 + 8, hotBtnHeight / 2);
		hotBtn.graphics.moveTo(hotBtnWidth / 2, (hotBtnHeight - 8) / 2);
		hotBtn.graphics.lineTo(hotBtnWidth / 2, (hotBtnHeight - 8) / 2 + 8);
	}
	
	private function updateHotBmpBox(lineColor:uint):void
	{
		bitmapBox.graphics.clear();
		bitmapBox.graphics.lineStyle(0, lineColor);
		bitmapBox.graphics.beginFill(0xffffff, .8);
		bitmapBox.graphics.drawRect(0, 0, hotBimapWidth, hotBimapHeight);
		
	}

	protected function onOver(evt:MouseEvent=null):void
	{
		
	}
	protected function onOut(evt:MouseEvent=null):void
	{
		
	}
	private function onHotAreaOver(evt:MouseEvent):void
	{
		updateHotBtnGraphics(0xffffff, 0);
		updateHotBmpBox(0xffffff);
	}
	private function onHotAreaOut(evt:MouseEvent=null):void
	{
		updateHotBtnGraphics(0, 0xffffff);
		updateHotBmpBox(0);
	}
	private function onComplete(evt:Event):void
	{
		_bitmap = evt.target.content as Bitmap;
		_bitmap.smoothing = true;
		_bitmap.x = 1;
		_bitmap.y = 1;
		_bitmap.mask = bitmapMask;
		
	    bitmapBox.addChild(_bitmap);
		setPhotoSize(_bitmap);
	}
	private function setPhotoSize(photo:Bitmap):void
	{
		DisplayUtil.setTheMostSuitableSize(photo, hotBimapWidth - 1, hotBimapHeight - 1);
	}
}
class PrevBtn extends BasicBtn
{
	private var _bmpBoxPosX:Number;
	public function PrevBtn(config:PhotoViewVO)
	{
		setConfig(config.hotAreaWidth,
		          config.hotAreaHeight,
				  config.hotBtnWidth,
				  config.hotBtnHeight,
				  config.hotBimapWidth,
				  config.hotBimapHeight);
		init();
	}
	private function init():void
	{
        _bmpBoxPosX = hotBtnWidth + 2;
		bitmapMask.x = _bmpBoxPosX+1;
	}
	override protected function onOver(evt:MouseEvent = null):void 
	{
		super.onOver(evt);
		if (!hotArea.contains(bitmapBox))
		    hotArea.addChild(bitmapBox);
		hotArea.setChildIndex(bitmapBox, 0);
		bitmapBox.x = -hotAreaWidth;
		Tweener.removeTweens(bitmapBox);
		Tweener.addTween(bitmapBox, { time:.5, x:_bmpBoxPosX } );
	}
	override protected function onOut(evt:MouseEvent = null):void 
	{
		super.onOut(evt);
		Tweener.addTween(bitmapBox, { time:.5, x:-hotAreaWidth,onComplete:onComplete } );
		function onComplete():void
		{
			if (hotArea.contains(bitmapBox))
		    hotArea.removeChild(bitmapBox);
		}
	}
}
class NextBtn extends BasicBtn
{
	private var _bmpBoxPosX:Number;
	public function NextBtn(config:PhotoViewVO)
	{
		setConfig(config.hotAreaWidth,
		          config.hotAreaHeight,
				  config.hotBtnWidth,
				  config.hotBtnHeight,
				  config.hotBimapWidth,
				  config.hotBimapHeight);
		init();
	}
	private function init():void
	{
		hotBtn.x = hotAreaWidth - hotBtnWidth;
		_bmpBoxPosX = hotAreaWidth - hotBtnWidth - hotBimapWidth - 3;
		bitmapMask.x = _bmpBoxPosX+1;
	}
	override protected function onOver(evt:MouseEvent = null):void 
	{
		super.onOver(evt);
		if (!hotArea.contains(bitmapBox))
		    hotArea.addChild(bitmapBox);
		hotArea.setChildIndex(bitmapBox, 0);
		bitmapBox.x = hotAreaWidth;
		Tweener.removeTweens(bitmapBox);
		Tweener.addTween(bitmapBox, { time:.5, x:_bmpBoxPosX } );
	}
	override protected function onOut(evt:MouseEvent = null):void 
	{
		super.onOut(evt);
		Tweener.addTween(bitmapBox, { time:.5, x:hotAreaWidth,onComplete:onComplete } );
		function onComplete():void
		{
			if (hotArea.contains(bitmapBox))
		    hotArea.removeChild(bitmapBox);
		}
	}
}
class InfoPanel extends Sprite
{
	private var _backgroundBmp:Bitmap
	private var _titleLabel:SimpleLabel;
	private var _contentLabel:SimpleLabel;
	public function InfoPanel()
	{
		configUI();
		drawLayout();
	}
	public function setData(data:PhotoVO):void
	{
		_titleLabel.text = data.title;
		_titleLabel.truncateToFitByWords(26);
		_contentLabel.text = data.content;
		_contentLabel.truncateToFitByLine(4);
	}
	private function configUI():void
	{
		_backgroundBmp = new Bitmap(AppGlobal.MATERIAL_RESOURCES.childInfoPanelBmpData);
		addChild(_backgroundBmp);
		
		_titleLabel = new SimpleLabel();
		_titleLabel.defaultTextFormat = new TextFormat('微软雅黑', 18, 0xffffff);
		_titleLabel.filters = [new BlurFilter(1.2, 1.2)];
		addChild(_titleLabel);
		
		_contentLabel = new SimpleLabel();
		var format:TextFormat = new TextFormat('宋体', 14, 0xffffff);
		format.leading = 1;
		format.letterSpacing = .5;
		_contentLabel.defaultTextFormat = format;
		_contentLabel.wordWrap = true;
		_contentLabel.multiline = true;
		_contentLabel.width = 500;
		addChild(_contentLabel);
	}
	private function drawLayout():void
	{
		_titleLabel.x = 10;
		_titleLabel.y = 10;
		
		_contentLabel.x = 10;
		_contentLabel.y = 40;
	}
}